package cn.stylefeng.guns.modular.knowledge.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.knowledge.entity.KnowledgeContent;
import cn.stylefeng.guns.modular.knowledge.model.params.KnowledgeContentParam;
import cn.stylefeng.guns.modular.knowledge.service.KnowledgeContentService;
import cn.stylefeng.guns.modular.stage.entity.SpecialtyType;
import cn.stylefeng.guns.modular.stage.service.SpecialtyTypeService;
import cn.stylefeng.guns.modular.stage.service.StageService;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;


/**
 * 知识点控制器
 *
 * @author CHENCHEN
 * @Date 2022-07-21 20:08:21
 */
@Controller
@RequestMapping("/knowledgeContent")
public class KnowledgeContentController extends BaseController {

    private String PREFIX = "/knowledgeContent";

    @Autowired
    private KnowledgeContentService knowledgeContentService;

    @Autowired
    private StageService stageService;

    /**
     * 跳转到主页面
     *
     * @author CHENCHEN
     * @Date 2022-07-21
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/knowledgeContent.html";
    }

    /**
     * 新增页面
     *
     * @author CHENCHEN
     * @Date 2022-07-21
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/knowledgeContent_add.html";
    }

    /**
     * 编辑页面
     *
     * @author CHENCHEN
     * @Date 2022-07-21
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/knowledgeContent_edit.html";
    }

    /**
     * 新增接口
     *
     * @author CHENCHEN
     * @Date 2022-07-21
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(KnowledgeContentParam knowledgeContentParam) {
        this.knowledgeContentService.add(knowledgeContentParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author CHENCHEN
     * @Date 2022-07-21
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(KnowledgeContentParam knowledgeContentParam) {
        this.knowledgeContentService.update(knowledgeContentParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author CHENCHEN
     * @Date 2022-07-21
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(KnowledgeContentParam knowledgeContentParam) {
        this.knowledgeContentService.delete(knowledgeContentParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author CHENCHEN
     * @Date 2022-07-21
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(KnowledgeContentParam knowledgeContentParam) {
        return ResponseData.success(this.knowledgeContentService.selectDetailById(knowledgeContentParam.getId()));
    }

    /**
     * 查询列表
     *
     * @author CHENCHEN
     * @Date 2022-07-21
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(KnowledgeContentParam knowledgeContentParam) {
        return this.knowledgeContentService.findPageBySpec(knowledgeContentParam);
    }

    /**
     * 跳转展示知识点列表
     * @param stageId
     * @param model
     * @return
     */
    @RequestMapping(value = "/knowledgeContentList/{stageId}")
    public String roleAssign(@PathVariable("stageId") Long stageId, Model model) {
        if (ToolUtil.isEmpty(stageId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        model.addAttribute("stageId", stageId);
        return "/stageData/knowledgeTable.html";
    }

    /**
     * 根据专业查询知识点
     * @param stageId
     * @return
     */
    @ResponseBody
    @RequestMapping("/getKnowledgeList")
    public ResponseData getKnowledgeList(Long stageId) {

        Map<String, Object> map = new HashMap<>();
        map.put("chapters", stageService.findChapters(stageId).getChapters());
        map.put("knowledgeContents", this.knowledgeContentService.getKnowledgeList(stageId));
        return ResponseData.success(map);
    }

}


